package Helicopter;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 * Actual game.
 * 
 * @author www.gametutorial.net
 */
@SuppressWarnings("unused")
public class Game {
    
    // Player - helicopter that is managed by player.
    private PlayerHelicopter player;
    
    // Enemy - helicopter that is trying to kill player.
    private EnemyHelicopter enemy;
    
    // Explosions
    private ArrayList<Animation> explosionsList;
    
    // Image for the sky color.
    private BufferedImage skyColorImg;
    
    // Font that we will use to write statistic to the screen.
	private Font font;
    
    // Statistics (destroyed enemies, run away enemies)
	private int runAwayEnemies;
	private int destroyedEnemies;
    
    public Game()
    {
        Framework.gameState = Framework.GameState.GAME_CONTENT_LOADING;
        
        Thread threadForInitGame = new Thread() {
            @Override
            public void run(){
                // Sets variables and objects for the game.
                Initialize();
                // Load game files (images, sounds, ...)
                LoadContent();
                
                Framework.gameState = Framework.GameState.PLAYING;
                
            }
        };
        threadForInitGame.start();
    }
    
    
   /**
     * Set variables and objects for the game.
     */
    private void Initialize()
    {
        
        player = new PlayerHelicopter(Framework.frameWidth / 4, Framework.frameHeight / 4);
        enemy  = new EnemyHelicopter (Framework.frameWidth / 2, Framework.frameHeight / 4);
        
        font = new Font("monospaced", Font.BOLD, 18);
        
        runAwayEnemies = 0;
        destroyedEnemies = 0;
        
    }
    
    /**
     * Load game files (images).
     */
    private void LoadContent()
    {
        try 
        {
            URL skyColorImgUrl = this.getClass().getResource("/resources/images/sky_color.jpg");
            skyColorImg = ImageIO.read(skyColorImgUrl);
            
        } 
        catch (IOException ex) 
        {
            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    
    /**
     * Restart game - reset some variables.
     */
    public void RestartGame()
    {
        player.Reset(Framework.frameWidth / 4, Framework.frameHeight / 4);
        
        explosionsList.clear();
        
        // Statistics
        runAwayEnemies = 0;
        destroyedEnemies = 0;
    }
    
    
    /**
     * Update game logic.
     * 
     * @param gameTime The elapsed game time in nanoseconds.
     * @param mousePosition current mouse position.
     */
    public void UpdateGame(long gameTime, Point mousePosition)
    {
        /* Player */
        // When player is destroyed and all explosions are finished showing we change game status.
        if( !isPlayerAlive() && explosionsList.isEmpty() ){
            Framework.gameState = Framework.GameState.GAMEOVER;
            return; // If player is destroyed, we don't need to do thing below.
        }
        
        // When a player is out of rockets and machine gun bullets, and all lists 
        // of bullets, rockets and explosions are empyt(end showing) we finish the game.
        if(player.numberOfAmmo <= 0 && 
           player.numberOfRockets <= 0 && 
           explosionsList.isEmpty())
        {
            Framework.gameState = Framework.GameState.GAMEOVER;
            return;
        }
        
        // If player is alive we update him.
        if(isPlayerAlive()){
            player.isMoving();
            player.Update();
        }
        if(isEnemyAlive())
        {
        	enemy.isMoving();
        	enemy.Update();
        }
        
    }
    
    /**
     * Draw the game to the screen.
     * 
     * @param g2d Graphics2D
     * @param mousePosition current mouse position.
     */
    public void Draw(Graphics2D g2d, long gameTime)
    {
        // Image for background sky color.
        g2d.drawImage(skyColorImg, 0, 0, Framework.frameWidth, Framework.frameHeight, null);
        
        if(isPlayerAlive())
            player.Draw(g2d);
        if(isEnemyAlive())
        	enemy.Draw(g2d);
    }
     
    /**
     * Check if player is alive. If not, set game over status.
     * 
     * @return True if player is alive, false otherwise.
     */
    private boolean isPlayerAlive()
    {
        if(player.health <= 0)
            return false;
        
        return true;
    }
    
    private boolean isEnemyAlive()
    {
        if(player.health <= 0)
            return false;
        
        return true;
    }
}
